Passed
Pull Request — develop (#758)
by Kevin Van
10:13 queued 04:10
created

helper.ts ➔ sortRankings   F

Complexity

Conditions 19

Size

Total Lines 54
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 54
rs 0.5999
c 0
b 0
f 0
cc 19

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like helper.ts ➔ sortRankings often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import { RankingDataTeamObject } from "../Types/Ranking"
2
3
export function mapPsdStatus(statusCode: number): string | null {
4
  const statusCodes = new Map([
5
    [0, `Gepland`],
6
    [1, `Forfait`],
7
    [2, `Afgelast`],
8
    [3, `Onderbroken`],
9
  ])
10
11
  return statusCodes.get(statusCode) || null
12
}
13
14
export function sortRankings(a: RankingDataTeamObject, b: RankingDataTeamObject) {
15
  // Rank lager: A stijgt in sortering.
16
  if (a.rank < b.rank) {
17
    return -1
18
  }
19
  if (a.rank > b.rank) {
20
    return 1
21
  }
22
  // Aantal overwinningen hoger: A stijgt in sortering.
23
  if (a.wins > b.wins) {
24
    return -1
25
  }
26
  if (a.wins < b.wins) {
27
    return 1
28
  }
29
  // Doelpuntensaldo beter: A stijgt in sortering.
30
  if (a.goalsScored - a.goalsConceded > b.goalsScored - b.goalsConceded) {
31
    return -1
32
  }
33
  if (a.goalsScored - a.goalsConceded < b.goalsScored - b.goalsConceded) {
34
    return 1
35
  }
36
  // Aantal gemaakte doelpunten hoger: A stijgt in sortering.
37
  if (a.goalsScored > b.goalsScored) {
38
    return -1
39
  }
40
  if (a.goalsScored < b.goalsScored) {
41
    return 1
42
  }
43
  // Aantal uitoverwinningen hoger: A stijgt in sortering.
44
  if (a.winsAway > b.winsAway) {
45
    return -1
46
  }
47
  if (a.winsAway < b.winsAway) {
48
    return 1
49
  }
50
  // Doelpuntensaldo op verplaatsing beter: A stijgt in sortering.
51
  if (a.goalsScoredAway - a.goalsConcededAway > b.goalsScoredAway - b.goalsConcededAway) {
52
    return -1
53
  }
54
  if (a.goalsScoredAway - a.goalsConcededAway < b.goalsScoredAway - b.goalsConcededAway) {
55
    return 1
56
  }
57
  // Aantal gemaakte doelpunten op verplaatsing hoger: A stijgt in sortering.
58
  if (a.goalsScoredAway > b.goalsScoredAway) {
59
    return -1
60
  }
61
  if (a.goalsScoredAway < b.goalsScoredAway) {
62
    return 1
63
  }
64
65
  return a.team?.club?.localName.localeCompare(b.team?.club?.localName)
66
}
67
68
export function replaceAll(source: string, search: string, replacement: string) {
69
  return source.replace(new RegExp(search, `g`), replacement)
70
}
71